<?php
require_once('../inc/func/isLogin.php');
require("../vendor/autoload.php");
require_once('../inc/data/dbfunc.php');
require_once('../inc/data/dbparam.php');


use PhpOffice\PhpSpreadsheet\IOFactory;

// 读取文件
$spreadsheet = IOFactory::load($_FILES["file"]["tmp_name"]);

// 读取表中第一个sheet
$workSheet = $spreadsheet->getSheet(0);
// 获取行数最大值
$HighestDataRow = $workSheet->getHighestDataRow();
$successNum = 0;
$failNum = 0;
// 遍历读取表中每一行数据
for ($i = 3; $i < $HighestDataRow; $i++) {
    $sco_usual = $workSheet->getCell("D{$i}")->getCalculatedValue();
    $sco_experiment = $workSheet->getCell("E{$i}")->getCalculatedValue();
    $sco_endterm = $workSheet->getCell("F{$i}")->getCalculatedValue();
    $sco_evaluate = $workSheet->getCell("G{$i}")->getCalculatedValue();
    $sco_stu = $workSheet->getCell("B{$i}")->getValue();
    // 插入数据
    $sql = "insert into huangcz_score (score_usual_grades,score_experiment_grades,score_final_grades,score_grade,score_stu,score_task)
            values (?,?,?,?,?,1)";
    $conn = conn_mysql();
    $stmt = $conn->prepare($sql);
    $bindStr = ['sssss', $sco_usual, $sco_experiment, $sco_endterm, $sco_evaluate, $sco_stu];
    $stmt->bind_param('ssssi', $sco_usual, $sco_experiment, $sco_endterm, $sco_evaluate, $sco_stu);
    $stmt->execute();
    echo $stmt->affected_rows;
    $stmt->affected_rows == 1 ? $successNum++ : $failNum++;
}

echo <<< js
    <script>
        alert("导入成功人数: {$successNum},   失败人数: {$failNum}")
        location="./tScoreIn.php";
    </script>
js;
